package com.biomedica.datasource;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.Iterator;


import org.json.JSONObject;

/**
 * 
 * @author Hytham ALOBYDI
 * @version  V1.0.0.0.71312
 * 
 */
/**  This Class will Take an Object and Generate a JSON reprentation for that OBject and vice virsa */
public class Object2JSON {

	/**   Object2JSON(Object c)  Take an Object and return a JSON reprentation of that Object 
	 * 
	 * @param  c                     The Object
	 * @return                        JSONObject
	 * @throws Exception 
	 *   
	 *   */
	public static  JSONObject  Object2JSON(Object c) throws Exception
	{
		JSONObject j=new JSONObject();
		try
		{
		Class C=c.getClass();
		Field[] f=C.getDeclaredFields();
		
		
		for(int i=0;i<f.length;i++)
		{
			
			String Name=f[i].getName();
			f[i].setAccessible(true);
			if(f[i].get(c)!=null)
			{
			String o=f[i].get(c).toString();
			j.put(Name, o);
			}
		}
		}
		catch(Exception err)
		{
			throw new Exception(err.getMessage());
		}
		return j;
	}

	/**   sObject2JSON(Object c)  Take an Object and return a JSON reprentation of that Object as String
	 * 
	 * @param  c                     The Object
	 * @return                        JSONObject String
	 * @throws JSONException 
	 *   
	 *   */
	public static  String  sObject2JSON(Object c) throws IllegalArgumentException, IllegalAccessException, ParseException
	{
		Class C=c.getClass();
		Field[] f=C.getDeclaredFields();
		
		JSONObject j=new JSONObject();
		for(int i=0;i<f.length;i++)
		{
			String Name=f[i].getName();
			f[i].setAccessible(true);
			String o=f[i].get(c).toString();
			j.put(Name, o);
		}
		return j.toString();
	}
	/** JSON2Object(String JSON,Object o)  Pars a JSON String and modefiy the passed Object
	
	 * @param JSON Json String
	 * @param o The Object to be Modefied
	 * @return The modifiedObject
	 * 
	 */
	public  static Object JSON2Object(String JSON,Object o) throws Exception
	{
		Class C=o.getClass();
		
		
		JSONObject j=new JSONObject(JSON);
		Iterator<?> keys = j.keys();
		 while( keys.hasNext() ){
	            String key = (String)keys.next();
	            String val=(String)j.get(key);
	            
	            try{
	            	
	            	Field f=C.getDeclaredField(key);
	            	f.set(o, val);
	            	
	            }
	            catch(Exception err)
	            {
	            	throw new Exception(err.getMessage());
	            }
		 }
		return o;
		
	}
/** JSON2Object(String JSON,Object o)  Pars a JSON Object and modefiy the passed Object
	
	 * @param JSON Json Object
	 * @param o The Object to be Modefied
	 * @return The modifiedObject
	 * 
	 */
	public  static Object getJSONFromObject(JSONObject j,Object o) throws Exception
	{
		Class C=o.getClass();
		
		
		
		Iterator<?> keys = j.keys();
		 while( keys.hasNext() ){
	            String key = (String)keys.next();
	            String val=(String)j.get(key);
	            
	            try{
	            	
	            	Field f=C.getDeclaredField(key);
	            	f.set(o, val);
	            	
	            }
	            catch(Exception err)
	            {
	            	throw new Exception(err.getMessage());
	            }
		 }
		return o;
		
	}
/** JSON2Object(String JSON,Object o)  Pars a JSON String and retturn a new Object of type C
	
	 * @param JSON Json String
	 * @param C  the Type to be generated
	 * @return The modifiedObject
	 * 
	 */
	public  static Object getObjectFromJSON(String JSON,Class C) throws Exception
	{
		
		Object o=C.newInstance();
		JSONObject j=new JSONObject(JSON);
		Iterator<?> keys = j.keys();
		 while( keys.hasNext() ){
	            String key = (String)keys.next();
	            String val=(String)j.get(key);
	            
	            try{
	            	
	            	Field f=C.getDeclaredField(key);
	            	f.set(o, val);
	            	
	            }
	            catch(Exception err)
	            {
	            	throw new Exception(err.getMessage());
	            }
		 }
		return o;
		
	}
/** JSON2Object(String JSON,Object o)  Pars a JSON String and retturn a new Object of type C
	
	 * @param JSON JsonObject
	 * @param C  the Type to be generated
	 * @return The New Object
	 * 
	 */
	public  static Object getObjectFromJSON(JSONObject JSON,Class C) throws Throwable
	{
		
		Object o=C.newInstance();
		JSONObject j=JSON;
		Iterator<?> keys = j.keys();
		 while( keys.hasNext() ){
	            String key = (String)keys.next();
	            String val=(String)j.get(key);
	            
	            try{
	            	
	            	Field f=C.getDeclaredField(key);
	            	f.set(o, val);
	            	
	            }
	            catch(Exception err)
	            {
	            	throw new Exception(err.getMessage());
	            }
		 }
		return o;
		
	}
	
	
	
	
}


	